package com.exam.project.business.service.impl;

import java.util.Date;
import java.util.List;

import com.exam.common.utils.DateUtils;
import com.exam.common.utils.SecurityUtils;
import com.exam.framework.security.LoginUser;
import com.exam.project.business.domain.Question;
import com.exam.project.business.dto.PracticeDto;
import com.exam.project.business.mapper.QuestionMapper;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.exam.project.business.mapper.PracticeRecordMapper;
import com.exam.project.business.domain.PracticeRecord;
import com.exam.project.business.service.IPracticeRecordService;

/**
 * 用户答题Service业务层处理
 *
 * @author qiuwenbing
 * @date 2025-04-03
 */
@Service
public class PracticeRecordServiceImpl implements IPracticeRecordService {
    @Autowired
    private PracticeRecordMapper practiceRecordMapper;
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询用户答题
     *
     * @param id 用户答题主键
     * @return 用户答题
     */
    @Override
    public PracticeRecord selectPracticeRecordById(Long id) {
        return practiceRecordMapper.selectPracticeRecordById(id);
    }

    /**
     * 查询用户答题列表
     *
     * @param practiceRecord 用户答题
     * @return 用户答题
     */
    @Override
    public List<PracticeRecord> selectPracticeRecordList(PracticeRecord practiceRecord) {
        return practiceRecordMapper.selectPracticeRecordList(practiceRecord);
    }

    /**
     * 新增用户答题
     *
     * @param practiceRecord 用户答题
     * @return 结果
     */
    @Override
    public int insertPracticeRecord(PracticeRecord practiceRecord) {
        practiceRecord.setCreateTime(DateUtils.getNowDate());
        return practiceRecordMapper.insertPracticeRecord(practiceRecord);
    }

    @Override
    public int markQuestion(Long questionId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 先查询
        PracticeRecord query = new PracticeRecord();
        query.setQuestionId(questionId);
        query.setUserId(loginUser.getUserId());
        List<PracticeRecord> list = practiceRecordMapper.selectPracticeRecordList(query);
        if (list != null && list.size() > 0) {
            PracticeRecord record = new PracticeRecord();
            record.setId(list.get(0).getId());
            record.setIsMarked(1);
            return practiceRecordMapper.updatePracticeRecord(record);
        } else {
            PracticeRecord record = new PracticeRecord();
            record.setUserId(loginUser.getUserId());
            record.setQuestionId(questionId);
            record.setIsMarked(1);
            return practiceRecordMapper.insertPracticeRecord(record);
        }
    }

    @Override
    public int unMarkQuestion(Long questionId) {
        PracticeRecord record = getPracticeRecord(questionId);
        if (record != null) {
            PracticeRecord update = new PracticeRecord();
            update.setId(record.getId());
            update.setIsMarked(0);
            return practiceRecordMapper.updatePracticeRecord(update);
        }
        return 0;
    }

    private PracticeRecord getPracticeRecord(Long questionId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PracticeRecord query = new PracticeRecord();
        query.setQuestionId(questionId);
        query.setUserId(loginUser.getUserId());
        PracticeRecord record = practiceRecordMapper.queryPracticeRecord(query);
        return record;
    }

    @Override
    public int submitPractice(PracticeDto dto) {
        Assert.notNull(dto.getQuestionId());
        Assert.notNull(dto.getQuestionType());
        Assert.notNull(dto.getAnswer());
        PracticeRecord record = getPracticeRecord(dto.getQuestionId());

        // 单选多选，填空  判断是否正确
        int correct = 0;
        Question question = questionMapper.selectQuestionById(dto.getQuestionId());
        if (question != null) {
            if ("single-choice".equals(question.getQuestionType()) || "multiple-choice".equals(question.getQuestionType()) || "blank-filling".equals(question.getQuestionType())) {
                if(question.getCorrectAnswer().equals(dto.getAnswer())){
                    correct = 1;
                }
            }
        }
        //
        if (record != null) {
            PracticeRecord pr = new PracticeRecord();
            pr.setId(record.getId());
            pr.setAnswerContent(dto.getAnswer());
            pr.setIsCorrect(correct);
            pr.setPracticeTime(new Date());
          return   practiceRecordMapper.updatePracticeRecord(pr);
        }else {
            PracticeRecord pr = new PracticeRecord();
            pr.setUserId(SecurityUtils.getUserId());
            pr.setQuestionId(dto.getQuestionId());
            pr.setAnswerContent(dto.getAnswer());
            pr.setIsCorrect(correct);
            pr.setPracticeTime(new Date());
          return   practiceRecordMapper.insertPracticeRecord(pr);
        }
    }

    /**
     * 修改用户答题
     *
     * @param practiceRecord 用户答题
     * @return 结果
     */
    @Override
    public int updatePracticeRecord(PracticeRecord practiceRecord) {
        practiceRecord.setUpdateTime(DateUtils.getNowDate());
        return practiceRecordMapper.updatePracticeRecord(practiceRecord);
    }

    /**
     * 批量删除用户答题
     *
     * @param ids 需要删除的用户答题主键
     * @return 结果
     */
    @Override
    public int deletePracticeRecordByIds(Long[] ids) {
        return practiceRecordMapper.deletePracticeRecordByIds(ids);
    }

    /**
     * 删除用户答题信息
     *
     * @param id 用户答题主键
     * @return 结果
     */
    @Override
    public int deletePracticeRecordById(Long id) {
        return practiceRecordMapper.deletePracticeRecordById(id);
    }
}
